package com.example.neusoft.yuejian_12.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.Listener.TrainingSignlListener;
import com.example.neusoft.yuejian_12.Model.TrainingModel;
import com.example.neusoft.yuejian_12.R;

/**
 * Created by admin on 2017/10/30.
 */

public class TrainingSignActivity extends AppCompatActivity implements View.OnClickListener {
    private RadioGroup rg;
    private RadioButton WeChat,Alipay;
    private ImageButton add,down;
    private TextView name,username,price,tel,intro,allprice;
    private EditText et_num;
    private Button buy;
    private ImageButton back;
    private int num=1,userid;
    String trname,trprice,notice,telephone,uname,Pay_type,Aprice,Anum,train_id;
    SharedPreferences sp;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainingsign);
        context = TrainingSignActivity.this;
        initViews();
        back.setOnClickListener(this);
        add.setOnClickListener(this);
        down.setOnClickListener(this);
        buy.setOnClickListener(this);
    }

    public void initViews() {
        /*获取用户*/
        sp=context.getSharedPreferences("userInfo",MODE_PRIVATE);
        userid=Integer.valueOf(sp.getString("user_id",null));
        telephone=sp.getString("user_tel",null);
        uname=sp.getString("user_name",null);

        Intent intent=getIntent();
        train_id=intent.getStringExtra("train_id");
        trname=intent.getStringExtra("train_name");
        trprice=intent.getStringExtra("train_price");
        notice=intent.getStringExtra("train_notice");
        name=(TextView)findViewById(R.id.traname);
        username= (TextView) findViewById(R.id.username);
        price= (TextView) findViewById(R.id.trainprice);
        tel= (TextView) findViewById(R.id.metel);
        intro= (TextView) findViewById(R.id.trainintro);
        allprice= (TextView) findViewById(R.id.all);
        et_num=(EditText)findViewById(R.id.num) ;
        add=(ImageButton)findViewById(R.id.add);
        down=(ImageButton)findViewById(R.id.down);
        buy= (Button) findViewById(R.id.btn_buy);
        rg=(RadioGroup) findViewById(R.id.radiogroup);
        WeChat=(RadioButton) findViewById(R.id.W_pay);
        Alipay=(RadioButton) findViewById(R.id.Z_pay);
        back= (ImageButton) findViewById(R.id.traback);
        name.setText(trname);
        price.setText(trprice);
        intro.setText(notice);
        tel.setText(telephone);
        username.setText(uname);
        allprice.setText((num*Integer.parseInt(trprice))+"(元)");
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.W_pay:
                        Pay_type="微信支付";
                        break;
                    case R.id.Z_pay:
                        Pay_type="支付宝支付";
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        /*结算*/
            case R.id.btn_buy:
                count();
                break;
         /*加*/
            case R.id.add:
                doAdd();
                break;
        /*减*/
            case R.id.down:
                doReduce();
                break;

            case R.id.traback:
                finish();
        }
    }

    //结算
    private void count() {
        trname = name.getText().toString();
        telephone = tel.getText().toString();
        Anum = et_num.getText().toString();
        notice = intro.getText().toString();
        Aprice = allprice.getText().toString();
        TrainingModel trainingModel = new TrainingModel();
        trainingModel.signTrain(trname,train_id,telephone,Anum,"1",Aprice,"训练",userid,trainingSignlListener);
    }

    TrainingSignlListener trainingSignlListener = new TrainingSignlListener() {
        @Override
        public void onResponse(Integer listBean) {
            Toast.makeText(TrainingSignActivity.this,"下单成功",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    /*减*/
    private void doReduce() {

        num=Integer.valueOf(et_num.getText().toString());
        if (0!=num)
            num--;
        et_num.setText(num+"");
        allprice.setText((num*Integer.parseInt(trprice))+"(元)");

    }

    /*加*/
    private void doAdd() {
        num=Integer.valueOf(et_num.getText().toString());
        num++;
        et_num.setText(num+"");
        allprice.setText((num*Integer.parseInt(trprice))+"(元)");
    }
}


